package com.dmwork.service.interaction.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmwork.common.base.result.R;
import com.dmwork.service.base.vo.WorkCommonVo;
import com.dmwork.service.interaction.entity.CmsExcellentWork;
import com.dmwork.service.interaction.entity.vo.CmsExcellentWorkVo;
import com.dmwork.service.interaction.feign.WorkFeignService;
import com.dmwork.service.interaction.mapper.CmsExcellentWorkMapper;
import com.dmwork.service.interaction.service.CmsExcellentWorkService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Transactional
@Service
public class CmsExcellentWorkServiceImpl extends ServiceImpl<CmsExcellentWorkMapper, CmsExcellentWork> implements CmsExcellentWorkService {

    @Autowired
    private WorkFeignService workFeignService;
    @Override
    public CmsExcellentWorkVo getExcellentWorkById(String id) {
        CmsExcellentWorkVo cmsExcellentWorkVo = new CmsExcellentWorkVo();
        //获取优秀作品表数据
        CmsExcellentWork cmsExcellentWork = this.getById(id);
        cmsExcellentWorkVo.setId(cmsExcellentWork.getId())
                .setWorkId(cmsExcellentWork.getWorkId())
                .setLevel(cmsExcellentWork.getLevel());

        //获取作品信息
        R<WorkCommonVo> res = workFeignService.getWorkInfoByWorkId(cmsExcellentWork.getWorkId());
        if (!res.getSuccess())
            return null;
        WorkCommonVo workCommonVo= res.getData().get("item");
        cmsExcellentWorkVo.setCover(workCommonVo.getCover()).setTitle(workCommonVo.getTitle());
        return cmsExcellentWorkVo;
    }

    @Override
    public List<CmsExcellentWorkVo> getExcellentWorkList() {
        List<CmsExcellentWork> cmsExcellentWorkList = this.list();
        LinkedList<String> workIdList = new LinkedList<>();
        for (CmsExcellentWork cmsExcellentWork : cmsExcellentWorkList) {
            workIdList.add(cmsExcellentWork.getWorkId());
        }
        R<List<WorkCommonVo>> res = workFeignService.getWorkListByWorkIds(workIdList);
        if (!res.getSuccess())
            return null;
        List<WorkCommonVo> workCommonVoList= res.getData().get("rows");
        List<CmsExcellentWorkVo> cmsExcellentWorkVoList=new ArrayList<>(cmsExcellentWorkList.size());
        for (CmsExcellentWork cmsExcellentWork : cmsExcellentWorkList) {
            for (WorkCommonVo workCommonVo : workCommonVoList) {
                if (workCommonVo.getId().equals(cmsExcellentWork.getWorkId())) {
                    cmsExcellentWorkVoList.add(new CmsExcellentWorkVo()
                            .setId(cmsExcellentWork.getId())
                            .setWorkId(cmsExcellentWork.getWorkId())
                            .setLevel(cmsExcellentWork.getLevel())
                            .setTitle(workCommonVo.getTitle())
                            .setCover(workCommonVo.getCover()));
                    break;
                }
            }
        }
        return cmsExcellentWorkVoList;
    }
}
